<?php
//------------------------------------------------------------ Contraintes d'accès de la page

	// on vérifie que cette page est appelée à partir de l'index et que l'utilisateur a le droit de consultation
	if(isset($_SESSION) && ($GLOBALS['USER']['level'] >= 2 || $GLOBALS['USER']['admin'])){
	
		// l'identifiant de la donnée à supprimer est indiqué par un entier
		if(isset($_GET['id']) && numericInt($_GET['id'])){
		
			$nb_taxons = valueExists((int)$_GET['id'], "CODETAXON", "taxon", $connexion);
			
			if($nb_taxons == 1){ // le taxon à afficher existe et son identifiant est unique
	
?>
<?php
/********************************* Règles :

	- id doit désigner un seul taxon
	- Le taxon doit exister dans la BDD

*****************************************/

	// récupération des informations du taxon
	$query_recuperation_taxon = "SELECT l1.CODENOMVERNACULAIRE AS idNom, l1.CODEAIREGEOGRAPHIQUE AS idRegionN, n.NOMVERNACULAIRE AS nom, r1.NOMAIREGEOGRAPHIQUE AS regionN, l2.CODETYPEBIOLOGIQUE AS idType, l2.CODEAIREGEOGRAPHIQUE AS idRegionT, tb.NOMTYPEBIOLOGIQUE AS type, r2.NOMAIREGEOGRAPHIQUE AS regionT, l3.CODEMILIEUBIOLOGIQUE AS idMilieu, l3.CODEAIREGEOGRAPHIQUE AS idRegionM, mb.NOMMILIEUBIOLOGIQUE AS milieu, r3.NOMAIREGEOGRAPHIQUE AS regionM, t.CODECLASSIFICATION AS idReference, c.NOMCLASSIFICATION AS reference, l4.CODEFAMILLE AS idFamille, l4.CODECLASSIFICATION AS idClassificationF, f.NOMFAMILLE AS famille, c1.NOMCLASSIFICATION AS classificationF, L5.CODESOUSFAMILLE AS idSousFamille, l5.CODECLASSIFICATION AS idClassificationSF, sf.NOMSOUSFAMILLE AS sousFamille, c2.NOMCLASSIFICATION AS classificationSF, l6.CODEGENRE AS idGenre, l6.CODECLASSIFICATION AS idClassificationG, g.NOMGENRE AS genre, c3.NOMCLASSIFICATION AS classificationG, l7.CODESOUSGENRE AS idSousGenre, l7.CODECLASSIFICATION AS idClassificationSG, sg.NOMSOUSGENRE AS sousGenre, c4.NOMCLASSIFICATION AS classificationSG, l8.CODEESPECE AS idEspece, l8.CODECLASSIFICATION AS idClassificationE, e.NOMESPECE AS espece, c5.NOMCLASSIFICATION AS classificationE, l9.CODESOUSESPECE AS idSousEspece, l9.CODECLASSIFICATION AS idClassificationSE, se.NOMSOUSESPECE AS sousEspece, c6.NOMCLASSIFICATION AS classificationSE, l10.CODEVARIETE AS idVariete, l10.CODECLASSIFICATION AS idClassificationV, v.NOMVARIETE AS variete, c7.NOMCLASSIFICATION AS classificationV, l11.CODECULTIVAR AS idCultivar, l11.CODEAUTEUR AS idAuteur, l11.NOMCULTIVAR AS cultivar, a.ABREVIATIONAUTEUR AS auteur, l12.CODEREFERENCE AS idDocument, r.TITREREFERENCE AS document, t.COMMENTAIRE AS commentaire ";
	$query_recuperation_taxon .= "FROM taxon t ";
	$query_recuperation_taxon .= "INNER JOIN classification c ON t.CODECLASSIFICATION = c.CODECLASSIFICATION ";
	$query_recuperation_taxon .= "LEFT JOIN se_nomme_couramment l1 INNER JOIN nom_vernaculaire n ON l1.CODENOMVERNACULAIRE = n.CODENOMVERNACULAIRE INNER JOIN aire_geographique r1 ON l1.CODEAIREGEOGRAPHIQUE = r1.CODEAIREGEOGRAPHIQUE ON t.CODETAXON = l1.CODETAXON ";
	$query_recuperation_taxon .= "LEFT JOIN est_de_type_biologique l2 INNER JOIN type_biologique tb ON l2.CODETYPEBIOLOGIQUE = tb.CODETYPEBIOLOGIQUE INNER JOIN aire_geographique r2 ON l2.CODEAIREGEOGRAPHIQUE = r2.CODEAIREGEOGRAPHIQUE ON t.CODETAXON = l2.CODETAXON ";
	$query_recuperation_taxon .= "LEFT JOIN se_trouve_en_milieu_biologique l3 INNER JOIN milieu_biologique mb ON l3.CODEMILIEUBIOLOGIQUE = mb.CODEMILIEUBIOLOGIQUE INNER JOIN aire_geographique r3 ON l3.CODEAIREGEOGRAPHIQUE = r3.CODEAIREGEOGRAPHIQUE ON t.CODETAXON = l3.CODETAXON ";
	$query_recuperation_taxon .= "LEFT JOIN est_de_famille l4 INNER JOIN famille f ON l4.CODEFAMILLE = f.CODEFAMILLE INNER JOIN classification c1 ON l4.CODECLASSIFICATION = c1.CODECLASSIFICATION ON t.CODETAXON = l4.CODETAXON ";
	$query_recuperation_taxon .= "LEFT JOIN est_de_sous_famille l5 INNER JOIN sous_famille sf ON l5.CODESOUSFAMILLE = sf.CODESOUSFAMILLE INNER JOIN classification c2 ON l5.CODECLASSIFICATION = c2.CODECLASSIFICATION ON t.CODETAXON = l5.CODETAXON ";
	$query_recuperation_taxon .= "LEFT JOIN appartient_a_genre l6 INNER JOIN genre g ON l6.CODEGENRE = g.CODEGENRE INNER JOIN classification c3 ON l6.CODECLASSIFICATION = c3.CODECLASSIFICATION ON t.CODETAXON = l6.CODETAXON ";
	$query_recuperation_taxon .= "LEFT JOIN appartient_a_sous_genre l7 INNER JOIN sous_genre sg ON l7.CODESOUSGENRE = sg.CODESOUSGENRE INNER JOIN classification c4 ON l7.CODECLASSIFICATION = c4.CODECLASSIFICATION ON t.CODETAXON = l7.CODETAXON ";
	$query_recuperation_taxon .= "LEFT JOIN est_de_espece l8 INNER JOIN espece e ON l8.CODEESPECE = e.CODEESPECE INNER JOIN classification c5 ON l8.CODECLASSIFICATION = c5.CODECLASSIFICATION ON t.CODETAXON = l8.CODETAXON ";
	$query_recuperation_taxon .= "LEFT JOIN est_de_sous_espece l9 INNER JOIN sous_espece se ON l9.CODESOUSESPECE = se.CODESOUSESPECE INNER JOIN classification c6 ON l9.CODECLASSIFICATION = c6.CODECLASSIFICATION ON t.CODETAXON = l9.CODETAXON ";
	$query_recuperation_taxon .= "LEFT JOIN a_comme_variete l10 INNER JOIN variete v ON l10.CODEVARIETE = v.CODEVARIETE INNER JOIN classification c7 ON l10.CODECLASSIFICATION = c7.CODECLASSIFICATION ON t.CODETAXON = l10.CODETAXON ";
	$query_recuperation_taxon .= "LEFT JOIN cultivar l11 INNER JOIN auteur a ON a.CODEAUTEUR = l11.CODEAUTEUR ON t.CODETAXON = l11.CODETAXON ";
	$query_recuperation_taxon .= "LEFT JOIN a_pour_support l12 INNER JOIN reference r ON r.CODEREFERENCE = l12.CODEREFERENCE ON t.CODETAXON = l12.CODETAXON ";
	$query_recuperation_taxon .= "WHERE t.CODETAXON = ".$_GET['id']." ";
	
	$result_recuperation_taxon = mysql_query($query_recuperation_taxon, $connexion) or logError("RECUPERATION TAXON-".$query_recuperation_taxon."-".mysql_error());
	
	$taxon = array(); // on créé un tableau contenant toutes les informations du taxon
	while($tab_recuperation_taxon = mysql_fetch_assoc($result_recuperation_taxon)){
		
		$taxon['noms'][$tab_recuperation_taxon['idNom']."-".$tab_recuperation_taxon['idRegionN']]['nom'] = $tab_recuperation_taxon['nom'];
		$taxon['noms'][$tab_recuperation_taxon['idNom']."-".$tab_recuperation_taxon['idRegionN']]['region'] = $tab_recuperation_taxon['regionN'];
		
		$taxon['types'][$tab_recuperation_taxon['idType']."-".$tab_recuperation_taxon['idRegionT']]['type'] = $tab_recuperation_taxon['type'];
		$taxon['types'][$tab_recuperation_taxon['idType']."-".$tab_recuperation_taxon['idRegionT']]['region'] = $tab_recuperation_taxon['regionT'];
		
		$taxon['milieux'][$tab_recuperation_taxon['idMilieu']."-".$tab_recuperation_taxon['idRegionM']]['milieu'] = $tab_recuperation_taxon['milieu'];
		$taxon['milieux'][$tab_recuperation_taxon['idMilieu']."-".$tab_recuperation_taxon['idRegionM']]['region'] = $tab_recuperation_taxon['regionM'];
		
		$taxon['classification']['id'] = $tab_recuperation_taxon['idReference'];
		$taxon['classification']['classification'] = $tab_recuperation_taxon['reference'];
		
		$taxon['familles'][$tab_recuperation_taxon['idClassificationF']]['id'] = $tab_recuperation_taxon['idFamille'];
		$taxon['familles'][$tab_recuperation_taxon['idClassificationF']]['famille'] = $tab_recuperation_taxon['famille'];
		$taxon['familles'][$tab_recuperation_taxon['idClassificationF']]['classification'] = $tab_recuperation_taxon['classificationF'];
		
		if(!is_null($tab_recuperation_taxon['sousFamille'])){
			$taxon['sousFamilles'][$tab_recuperation_taxon['idClassificationSF']]['id'] = $tab_recuperation_taxon['idSousFamille'];
			$taxon['sousFamilles'][$tab_recuperation_taxon['idClassificationSF']]['sousFamille'] = $tab_recuperation_taxon['sousFamille'];
			$taxon['sousFamilles'][$tab_recuperation_taxon['idClassificationSF']]['classification'] = $tab_recuperation_taxon['classificationSF'];
		}
		
		$taxon['genres'][$tab_recuperation_taxon['idClassificationG']]['id'] = $tab_recuperation_taxon['idGenre'];
		$taxon['genres'][$tab_recuperation_taxon['idClassificationG']]['genre'] = $tab_recuperation_taxon['genre'];
		$taxon['genres'][$tab_recuperation_taxon['idClassificationG']]['classification'] = $tab_recuperation_taxon['classificationG'];
		
		if(!is_null($tab_recuperation_taxon['sousGenre'])){
			$taxon['sousGenres'][$tab_recuperation_taxon['idClassificationSG']]['id'] = $tab_recuperation_taxon['idSousGenre'];
			$taxon['sousGenres'][$tab_recuperation_taxon['idClassificationSG']]['sousGenre'] = $tab_recuperation_taxon['sousGenre'];
			$taxon['sousGenres'][$tab_recuperation_taxon['idClassificationSG']]['classification'] = $tab_recuperation_taxon['classificationSG'];
		}
		
		$taxon['especes'][$tab_recuperation_taxon['idClassificationE']]['id'] = $tab_recuperation_taxon['idEspece'];
		$taxon['especes'][$tab_recuperation_taxon['idClassificationE']]['espece'] = $tab_recuperation_taxon['espece'];
		$taxon['especes'][$tab_recuperation_taxon['idClassificationE']]['classification'] = $tab_recuperation_taxon['classificationE'];
		
		if(!is_null($tab_recuperation_taxon['sousEspece'])){
			$taxon['sousEspeces'][$tab_recuperation_taxon['idClassificationSE']]['id'] = $tab_recuperation_taxon['idSousEspece'];
			$taxon['sousEspeces'][$tab_recuperation_taxon['idClassificationSE']]['sousEspece'] = $tab_recuperation_taxon['sousEspece'];
			$taxon['sousEspeces'][$tab_recuperation_taxon['idClassificationSE']]['classification'] = $tab_recuperation_taxon['classificationSE'];
		}
		
		$taxon['varietes'][$tab_recuperation_taxon['idClassificationV']]['id'] = $tab_recuperation_taxon['idVariete'];
		$taxon['varietes'][$tab_recuperation_taxon['idClassificationV']]['variete'] = $tab_recuperation_taxon['variete'];
		$taxon['varietes'][$tab_recuperation_taxon['idClassificationV']]['classification'] = $tab_recuperation_taxon['classificationV'];
		
		if(!is_null($tab_recuperation_taxon['cultivar'])){
			$taxon['cultivars'][$tab_recuperation_taxon['idCultivar']]['cultivar'] = $tab_recuperation_taxon['cultivar'];
			$taxon['cultivars'][$tab_recuperation_taxon['idCultivar']]['idAuteur'] = $tab_recuperation_taxon['idAuteur'];
			$taxon['cultivars'][$tab_recuperation_taxon['idCultivar']]['auteur'] = $tab_recuperation_taxon['auteur'];
		}
		
		if(!is_null($tab_recuperation_taxon['document'])){
			$taxon['references'][$tab_recuperation_taxon['idDocument']]['id'] = $tab_recuperation_taxon['idDocument'];
			$taxon['references'][$tab_recuperation_taxon['idDocument']]['titre'] = $tab_recuperation_taxon['document'];
		}
		
		$taxon['commentaire'] = $tab_recuperation_taxon['commentaire'];
	
	}
	
	mysql_free_result($result_recuperation_taxon);
	
?>
<h3>Détail d'un taxon</h3>
<table class="fiche" style="width: 100%;">
	<tr>
		<td colspan="2">
			<b><?php echo htmlentities(capitalise($taxon['familles'][$taxon['classification']['id']]['famille']), ENT_NOQUOTES, "UTF-8"); ?> <?php echo htmlentities(capitalise($taxon['genres'][$taxon['classification']['id']]['genre']), ENT_NOQUOTES, "UTF-8"); ?> <?php echo htmlentities($taxon['especes'][$taxon['classification']['id']]['espece'], ENT_NOQUOTES, "UTF-8"); ?> <?php if (isset($taxon['sousEspeces'][$taxon['classification']['id']]['sousEspece'])) { echo htmlentities($taxon['sousEspeces'][$taxon['classification']['id']]['sousEspece'], ENT_NOQUOTES, "UTF-8"); } ?> <?php if (isset($taxon['varietes'][$taxon['classification']['id']]['variete'])) { echo htmlentities($taxon['varietes'][$taxon['classification']['id']]['variete'], ENT_NOQUOTES, "UTF-8"); } ?></b>
		</td>
	</tr>
	<tr>
		<th colspan="2">Informations</th>
	</tr>
	<tr>
		<td class="etiquette">Nom:</td>
		<td>
			<ul>
				<?php
					foreach($taxon['noms'] as $nom){
						if (isset ($nom['nom'])) {
                     echo "<li>".htmlentities($nom['nom'], ENT_NOQUOTES, "UTF-8")." (".htmlentities($nom['region'], ENT_NOQUOTES, "UTF-8").")</li>";
                  } else {
                     echo "<i>Aucun</i>";
                  }
					}
					unset($nom);
				?>
			</ul>
		</td>
	</tr>
	<tr>
		<td class="etiquette">Type Bio.:</td>
		<td>
			<ul>
				<?php
					foreach($taxon['types'] as $type){
                  if (isset ($type['type'])) {
                     echo "<li>".htmlentities(mb_strtolower($type['type']), ENT_NOQUOTES, "UTF-8")." (".htmlentities($type['region'], ENT_NOQUOTES, "UTF-8").")</li>";
                  } else {
                     echo "<i>Aucun</i>";
                  }
               }
					unset($type);
				?>
			</ul>
		</td>
	</tr>
	<tr>
		<td class="etiquette">Milieu:</td>
		<td>
			<ul>
				<?php
					foreach($taxon['milieux'] as $milieu){
                  if(isset($milieu['milieu'])){
                     echo "<li>".htmlentities(mb_strtolower($milieu['milieu']), ENT_NOQUOTES, "UTF-8")." (".htmlentities($milieu['region'], ENT_NOQUOTES, "UTF-8").")</li>";
                  } else {
                     echo "<i>Aucun</i>";
                  }
               }
					unset($milieu);
				?>
			</ul>
		</td>
	</tr>
	<tr>
		<th colspan="2">Classification</th>
	</tr>
	<tr>
		<td style="width: 27%" class="etiquette">Référence:</td>
		<td style="width: 73%"><?php echo htmlentities(capitalise($taxon['classification']['classification']), ENT_NOQUOTES, "UTF-8"); ?></td>
	</tr>
	<tr>
		<td class="etiquette">Famille:</td>
		<td>
			<ul>
				<?php
					foreach($taxon['familles'] as $famille){
						echo "<li><a href=\"popup.php?p=gestion/famille_affiche_popup&amp;id=".$famille['id']."\" onclick=\"popup(this.href, 400, 250); return false;\">".htmlentities(capitalise($famille['famille']), ENT_NOQUOTES, "UTF-8")."</a> (<i>".htmlentities($famille['classification'], ENT_NOQUOTES, "UTF-8")."</i>)</li>";
					}
					unset($famille);
				?>
			</ul>
		</td>
	</tr>
	<tr>
		<td class="etiquette">Sous-famille:</td>
		<td>
			<?php
				if(isset($taxon['sousFamilles'])){
					echo "<ul>";
					foreach($taxon['sousFamilles'] as $sousFamille){
						echo "<li><a href=\"popup.php?p=gestion/sousfamille_affiche_popup&amp;id=".$sousFamille['id']."\" onclick=\"popup(this.href, 550, 400); return false;\">".htmlentities(capitalise($sousFamille['sousFamille']), ENT_NOQUOTES, "UTF-8")."</a> (<i>".htmlentities($sousFamille['classification'], ENT_NOQUOTES, "UTF-8")."</i>)</li>";
					}
					unset($sousFamille);
					echo "</ul>";
				}else{
					echo "<i>Aucune appartenance</i>";
				}
			?>
		</td>
	</tr>
	<tr>
		<td class="etiquette">Genre:</td>
		<td>
			<ul>
				<?php
					foreach($taxon['genres'] as $genre){
						echo "<li><a href=\"popup.php?p=gestion/genre_affiche_popup&amp;id=".$genre['id']."\" onclick=\"popup(this.href, 550, 400); return false;\">".htmlentities(capitalise($genre['genre']), ENT_NOQUOTES, "UTF-8")."</a> (<i>".htmlentities($genre['classification'], ENT_NOQUOTES, "UTF-8")."</i>)</li>";
					}
					unset($genre);
				?>
			</ul>
		</td>
	</tr>
	<tr>
		<td class="etiquette">Sous-genre:</td>
		<td>
			<?php
				if(isset($taxon['sousGenres'])){
					echo "<ul>";
					foreach($taxon['sousGenres'] as $sousGenre){
						echo "<li><a href=\"popup.php?p=gestion/sousgenre_affiche_popup&amp;id=".$sousGenre['id']."\" onclick=\"popup(this.href, 550, 400); return false;\">".htmlentities(capitalise($sousGenre['sousGenre']), ENT_NOQUOTES, "UTF-8")."</a> (<i>".htmlentities($sousGenre['classification'], ENT_NOQUOTES, "UTF-8")."</i>)</li>";
					}
					unset($sousGenre);
					echo "</ul>";
				}else{
					echo "<i>Aucune appartenance</i>";
				}
			?>
		</td>
	</tr>
	<tr>
		<td class="etiquette">Espèce:</td>
		<td>
			<ul>
				<?php
					foreach($taxon['especes'] as $espece){
						echo "<li><a href=\"popup.php?p=gestion/espece_affiche_popup&amp;id=".$espece['id']."\" onclick=\"popup(this.href, 550, 400); return false;\">".htmlentities($espece['espece'], ENT_NOQUOTES, "UTF-8")."</a> (<i>".htmlentities($espece['classification'], ENT_NOQUOTES, "UTF-8")."</i>)</li>";
					}
					unset($espece);
				?>
			</ul>
		</td>
	</tr>
	<tr>
		<td class="etiquette">Sous-espèce:</td>
		<td>
			<?php
				if(isset($taxon['sousEspeces'])){
					echo "<ul>";
					foreach($taxon['sousEspeces'] as $sousEspece){
						echo "<li><a href=\"popup.php?p=gestion/sousespece_affiche_popup&amp;id=".$sousEspece['id']."\" onclick=\"popup(this.href, 550, 400); return false;\">".htmlentities($sousEspece['sousEspece'], ENT_NOQUOTES, "UTF-8")."</a> (<i>".htmlentities($sousEspece['classification'], ENT_NOQUOTES, "UTF-8")."</i>)</li>";
					}
					unset($sousEspece);
					echo "</ul>";
				}else{
					echo "<i>Aucune appartenance</i>";
				}
			?>
		</td>
	</tr>
	<tr>
		<td class="etiquette">Variété:</td>
		<td>
				<?php
               if(isset($taxon['varietes']['variete'])){
   					echo "<ul>";
   					foreach($taxon['varietes'] as $variete){
   						echo "<li><a href=\"popup.php?p=gestion/variete_affiche_popup&amp;id=".$variete['id']."\" onclick=\"popup(this.href, 550, 400); return false;\">".htmlentities($variete['variete'], ENT_NOQUOTES, "UTF-8")."</a> (<i>".htmlentities($variete['classification'], ENT_NOQUOTES, "UTF-8")."</i>)</li>";
   					}
   					unset($variete);
   					echo "</ul>";
   				}else{
   					echo "<i>Aucune appartenance</i>";
   				}
				?>
			</ul>
		</td>
	</tr>
	<tr>
		<td class="etiquette">Cultivar:</td>
		<td>
			<?php
				if(isset($taxon['cultivars'])){
					echo "<ul>";
					foreach($taxon['cultivars'] as $cultivar){
						echo "<li>".htmlentities($cultivar['cultivar'], ENT_NOQUOTES, "UTF-8")." (<i>découvert par</i> <a href=\"popup.php?p=gestion/auteur_affiche_popup&amp;id=".$cultivar['idAuteur']."\" onclick=\"popup(this.href, 400, 250); return false;\">".htmlentities($cultivar['auteur'], ENT_NOQUOTES, "UTF-8")."</a>)</li>";
					}
					unset($cultivar);
					echo "</ul>";
				}else{
					echo "<i>Ce taxon n'est pas un cultivar</i>";
				}
			?>
		</td>
	</tr>
	<tr>
		<th colspan="2">Documentation</th>
	</tr>
	<tr>
		<td class="etiquette">Document(s):</td>
		<td colspan="2">
			<?php
				if(isset($taxon['references'])){
					echo "<ul>";
					foreach($taxon['references'] as $reference){
						echo "<li><a href=\"popup.php?p=gestion/reference_affiche_popup&amp;id=".$reference['id']."\" onclick=\"popup(this.href, 500, 300); return false;\">".htmlentities($reference['titre'], ENT_NOQUOTES, "UTF-8")."</a></li>";
					}
					unset($reference);
					echo "</ul>";
				}else{
					echo "<i>Aucun document relatif à ce taxon</i>";
				}
			?>
		</td>
	</tr>
	<tr>
		<th colspan="2">Commentaire</th>
	</tr>
	<tr>
		<td class="texte" colspan="2">
      <?php 
         if ($taxon['commentaire'] !="") {
            echo nl2br(htmlentities($taxon['commentaire'], ENT_NOQUOTES, "UTF-8"));
         } else {
            echo "<i>Aucun Commentaire</i>";
         }
      
      ?>
      </td>
	</tr>
</table>
<?php
//------------------------------------------------------------ Accès refusé à la page

			}else{
				afficherStatut("Le taxon n'existe pas", 3); // appel à la fonction afficherStatut() définie dans fonctions.php
			}

		}else{
			afficherStatut("Le taxon n'est pas ou mal indiqué", 3); // appel à la fonction afficherStatut() définie dans fonctions.php
		}

	}else{
		if(isset($_SESSION)){
			include("pages/401.htm");
		}
	}

?>